package com.open.capacity.finance.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.finance.dto.AuditModelBaseResult;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.finance.entity.AuditModelWarnPlanDetail;
import com.open.capacity.finance.service.AbstractAuditMakeResultFactory;
import com.open.capacity.finance.service.AuditModelService;
import com.open.capacity.user.model.SysCompany;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.finance.entity.AuditModelWarnPlan;
import com.open.capacity.finance.service.AuditModelWarnPlanService;

import javax.validation.Valid;

import com.open.capacity.commons.PageResult;

/**
 * 模型指标执行计划
 *
 * @author
 * @email
 * @date 2023-04-28 10:02:39
 */
@RestController
@RequestMapping("auditmodelwarnplans")
@Api(tags = "模型指标执行计划")
public class AuditModelWarnPlanController {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private AuditModelWarnPlanService auditModelWarnPlanService;

    @Autowired
    private AuditModelService auditModelService;

    private static final String EXCUTEAUDITCLASSPATHNAME = "com.open.capacity.finance.service.impl.AuditModelActuator%sServiceImpl";

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('audit_model_warn_plan:auditmodelwarnplan:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = auditModelWarnPlanService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询公司tree
     *
     * @param parentId
     * @return
     */
    @GetMapping("/tree")
    public Result<List<TreeVo>> treePlans(@RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("status", 1);
        List<AuditModelWarnPlan> plans = auditModelWarnPlanService.findAll(params);
        List<TreeVo> treeList = new TreeList();
        plans.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setTitle(m.getName());
            node.setValue(m.getId().toString());
            node.setShowcheck(false);
            node.setCheckstate(0);
            node.setIsexpand(false);
            node.setComplete(false);
            node.setParentId("0");
            treeList.add(node);
        });
        if (CollectionUtils.isNotEmpty(treeList) && treeList.size() == 1) {
            treeList.get(0).setParentId("0");
        }
        return Result.succeed(treeList);
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('audit_model_warn_plan:auditmodelwarnplan:list')")
    public Result findById(String id) {
        return Result.succeed(auditModelWarnPlanService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @LogAnnotation(module = "预警执行计划", name = "编辑")
    @PreAuthorize("hasAnyAuthority('audit_model_warn_plan:auditmodelwarnplan:save')")
    public Result save(@RequestBody @Valid AuditModelWarnPlan auditModelWarnPlan, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            auditModelWarnPlanService.save(auditModelWarnPlan);
        } else {
            auditModelWarnPlan.setId(keyValue);
            auditModelWarnPlanService.update(auditModelWarnPlan);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @LogAnnotation(module = "预警执行计划", name = "删除")
    @PreAuthorize("hasAnyAuthority('audit_model_warn_plan:auditmodelwarnplan:delete')")
    public Result delete(@PathVariable String id) {
        auditModelWarnPlanService.delete(id);
        return Result.succeed("删除成功");
    }


    /**
     * 执行分析
     */
    @RequestMapping("/analyse")
    @LogAnnotation(module = "预警执行计划", name = "执行")
    @PreAuthorize("hasAnyAuthority('audit_model_warn_plan:auditmodelwarnplan:analyse')")
    public Result analyseModel(String id, Map<String, Object> param) {
        Assert.isTrue(StringUtils.isNotEmpty(id), "请选择需要执行分析的计划");
        int count = auditModelWarnPlanService.analyse(id, param);

        return Result.succeed("执行完成");
    }

}
